<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>日志记录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }
        .container {
            width: 80%;
            margin: 20px auto;
            overflow: hidden;
        }
        header {
            background: #333;
            color: #fff;
            padding: 20px;
            text-align: center;
        }
        nav {
            display: flex;
            justify-content: center;
            background: #555;
            padding: 10px 0;
        }
        nav a {
            color: white;
            text-decoration: none;
            padding: 0 15px;
        }
        nav a:hover {
            background: #444;
        }
        .logs {
            background: #fff;
            padding: 20px;
            margin-bottom: 20px;
            border: 1px solid #ddd;
        }
        .log-entry {
            border-bottom: 1px solid #eee;
            padding: 10px;
        }
        .log-entry:last-child {
            border-bottom: none;
        }
        .log-time {
            color: #888;
            font-size: 0.9em;
        }
        footer {
            background: #333;
            color: #fff;
            text-align: center;
            padding: 10px 0;
            position: relative;
            bottom: 0;
            width: 100%;
        }
    </style>
</head>
<body>
    <header>
        <h1>日志记录</h1>
    </header>
    <nav>
        <a href="index.html">首页</a>
    </nav>
    <div class="container">
        <div class="logs">
            <h2>系统日志</h2>
            <div class="log-entry">
                <span class="log-time">2024-04-01 10:00:00</span>
                <p>用户123登录系统</p>
            </div>
            <div class="log-entry">
                <span class="log-time">2024-04-01 10:05:00</span>
                <p>用户123更新了新闻文章</p>
            </div>
        </div>
    </div>
    <footer>
        <p>版权所有 © 2024 新闻网站</p>
    </footer>
</body>
<script src="./js/jquery-3.7.1.min.js"></script>
<script>
    getLogList()
    function getLogList() {
        $.ajax({
            url: "http://www.dlrb.com:8082/getLogList.php",
            type: "GET",
            xhrFields: {
                withCredentials: true
            },
            dataType: "json",
            success: (resp) => {
                if (resp.code == 200) {
                    let context = ""
                    for(let item of resp.data){
                        context += `
                        <div class="log-entry">
                            <span class="log-time">${item.time}</span>
                            <p>用户${item.username} ${item.do} ${item.target}</p>
                        </div>
                        `
                    }
                    $(".logs").html(context)
                } else {
                    alert(resp.msg)
                }
            },
            error: (resp) => {
                alert(resp.msg)
            }
        })
    }

    function cookieToJson(cookieString) {
        // 处理空字符串
        if (!cookieString || typeof cookieString !== 'string') {
            return {};
        }
        // 分割 Cookie 字符串
        const cookies = cookieString.split(';');
        const result = {};
        // 遍历每个 Cookie
        for (const cookie of cookies) {
            // 跳过空字符串
            if (!cookie.trim()) {
                continue;
            }
            // 查找第一个等号位置
            const equalsIndex = cookie.indexOf('=');

            // 如果没有等号，跳过这个 Cookie
            if (equalsIndex === -1) {
                continue;
            }
            // 提取键和值
            const name = cookie.substring(0, equalsIndex).trim();
            let value = cookie.substring(equalsIndex + 1).trim();
            // 处理可能的 URL 编码
            try {
                value = decodeURIComponent(value);
            } catch (e) {
                // 如果解码失败，使用原始值
                console.warn(`Failed to decode cookie value: ${value}`, e);
            }
            // 添加到结果对象
            result[name] = value;
        }
        return result;
    }
</script>
</html>